package jsyf.oa.service.tmpevent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import jsyf.oa.dao.tmpevent.EventCopierDao;
import jsyf.oa.entity.tmpEvent.EventCopierEntity;
import jsyf.oa.entity.tmpEvent.EventEntity;
import jsyf.oa.facade.tmpevent.EventCopierFacade;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class EventCopierService implements EventCopierFacade {
    @Autowired
    private EventCopierDao eventcopierDao;

    @Override
    public String save(String jsonData) {
        EventEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), EventEntity.class);
        String mainId = entity.getMain().getId();
        eventcopierDao.removeByCondition(" AND FK_EVENT_ID = ? ", new String[]{mainId});
        //添加
        List<EventCopierEntity> eventCopierEntityList = entity.getCopiers();
        for (int i = 0; i < eventCopierEntityList.size(); i++) {
            eventCopierEntityList.get(i).setFkEventId(mainId);
            this.eventcopierDao.add(eventCopierEntityList.get(i));
        }
        return entity.getMain().getId();
    }

    @Override
    public List<JSONObject> getByEventId(String moneyId) {
        return eventcopierDao.getJSONListByCondition(" AND FK_EVENT_ID = ?", new Object[]{moneyId});
    }
}
